Poster living in Madrid checking in.
Sounds like a good plan. Train travel is safe and fast. The AVE can be a bit on the expensive side and I’ve found you can usually get MAD-BCN flights for the same amount if you book in advance.
Barcelona is a wonderful city. I love the Gaudi sites and I consider the Parque Guell and la Sagrada Familia as must see destinations. There is also the Spanish Village - “Poble Espanyol” and the marina area. I think 2 days is a minimum you’d want to stay.
Madrid is cosmopolitan with lots of museums if that is your thing. There’s the Royal Palace, the Plaza Mayor to see/hang while roaming around the old parts of the city. Madrid really comes alive at night. Lots of tapas and drinks make for fun until the wee hours.
I’m not so convinced about Valencia. There are some interesting parts of course and some beautiful beach towns outside the city, but I would think about extending BCN and/or MAD. If you extend MAD you can think about including a visit to nearby Toledo and Segovia -very recommendable (train service <1hr).
There is also a possibility to opt to include a visit to Granada which is my personal favorite city in Spain or up to Bilbao or San Sebastian. There’s no AVE train service to these places yet so you’d need to investigate logistics.
En fin… Travel itineraries are based on personal decisions/priorities so it is hard to recommend specifics without knowing who I’m talking to. Safe and enjoyable travels.