My semester in Madrid is something that I still reminisce about 6 years later. It was probably the boldest thing I'd ever done at that point, and although it was very expensive, it was worth it in terms of experiencing another culture and learning Spanish, both of which will stay with you for a lifetime. If you're turned off by the price tag but have a nagging wanderlust for Madrid/Spain that can't be ignored, I would take a leap of faith and try to find a way to make it work. You won't regret it later (well, until your loans are paid off anyways ;) )
I don't think there's a right or wrong reason to go study abroad. I was more into partying and exploring than I was about studying, and I think that's totally fine. You will feel very free so do what you need to do, and just try to find your balance. I was still able to pick up a lot of Spanish through courses and these skills transfer over to Latin American Spanish too. The only things I would change if I were to do it again would be to not hang out with Americans 24/7 and to see more parts of the city than home/school/major sights.
Madrid is a gorgeous city, and a great first-European-city-visited if this is your first time in Europe. The metro system puts every system here to shame, and the famous nightlife lives up to its reputation. The culture is somewhat familiar but also has stark differences. If you love SF for its food, art, music, parks, neighborhoods, scenery, etc., you will love Madrid for the same reasons. Most of the negative experiences I experienced had to do with being Asian in Spain but there was never anything violent or overly hostile. If you are non-European looking, be warned that Spain is far less cosmopolitan and tolerant than the Bay Area.. in the grand picture though, I think this was a good thing for me to experience. You will also have to watch out for your stuff constantly. Madrid is a great place to travel to other places for cheap (I took a 30 euro flight to Morocco!), and there are enough places to visit within Spain to last three months although you'll probably want to go to other countries too. To my surprise, I hated Barcelona (or rather, Barcelonians) but fell in love with Andalucía.
If any two of these apply to you: you want to experience living on your own, you want to learn Spanish, you want to travel Europe, you want to experience living in another culture -- I think it warrants the price tag. I took out a loan and saw every penny I made for over a year after go back towards the loan. This part is definitely not fun, but ultimately I feel like it was worth it. If you have money lying around to spend on phones, clothes, etc., spend it on this instead!