Yes, at least 99.5% of all public tap water in Spain is safe to drink according to international water quality standards. But there are issues such as taste, odor chlorine by-products, microplastics and local pipe contaminants.
The water companies are obliged to provide frequent water quality reports and alert customers if there is any non-compliance. Therefore there is minimal risk of getting sick from bacteria in water from the tap, public drinking fountains, at restaurants, etc.
One challenge is that Spanish water providers (just like most other countries in the world) use chlorine to sterilize tap water. For this reason, there can be a certain chemical taste to the water. It is not dangerous, and you usually get used to it after a while.
If you live near the coast, you may have higher levels of sediment like fine sand as well as more minerals in your tap water. Again, this is not dangerous, but the water may not taste as good.
If you don’t like the taste of tap water or worry about contaminants get a portable and easy to install water filter in Spain such as TAPP (price from 39 EUR for 3 months for). This has the bonus of saving money and reducing plastic waste.