That is not the issue. The issue is supply. People keep blocking construction of new housing and so prices go up and therefore it becomes an investments. increase the supply and it would no longer be an investment and you could not charge high rents
This is a very "Econ 101" way of looking at the problem. If you've ever been to Paris, Amsterdam, Lisbon, London, Barcelona, you quickly realize you can't "build more," as the desirable city locations are saturated.
The problem is that the supply/demand curves are being manipulated by, as mentioned, property owners which are destroying the fabric of their respective city via short-term rentals (or, by just owning an appreciating empty property), which, in turn, increases rents and prices for housing en masse. All of a sudden, a "boring" property becomes an investment that generates revenue. All of a sudden, hedge funds, banks, etc. want in on the action. There's money to be made, after all!
While this used to be the case in the past (if you bought, for example, farmland, or a factory, or something), the financialization of housing is a relatively new phenomenon.
Expats and global financial markets further complicate this: how can an average Spaniard that makes $50k compete with a California "digital nomad" that makes $150k? Obviously, we need to have some sensible rules that protect the citizens with heritage and history there.