Yes, absolutely.
The Spanish economy thrives on the tourism industry, and with investors coming in from various other countries.
For this reason, Spain actually encourages buyers, both resident and non-resident to buy property in Spain
You will need to get a little bit of paperwork in place first, in particular, an NIE number which is basically a national identification number for foreigners, but we´ll go other that later
