Hi, RichardLouisa,
I think that in part the answer depends on what time you need to be in Madrid and whether you plan to spend time there. I always fly in and out of Madrid and I usually spend a day or two in the city. If I take the overnight train and get a bed, I have a full day in Santiago, leave at night, get to Madrid early the next morning. You don't say how old you are, but if you're over 60 (?) you are eligible for 40% off standard train fares, and that's an excellent bargain.
This year I have to be home and can't spend the time in Madrid, so I'm flying from Santiago. There is an early morning flight that gets me in Madrid in time to get on lots of flights back to the US (most leave between 11 and 1 pm) (not sure where you're from). That's certainly the easiest, but not my preference, because I love Madrid.
It would be possible to take the overnight train, arrive in Madrid (scheduled around 8 am), and make it to the airport for a flight later that morning, but it might be a close call if there are any delays.
Buen camino, Laurie