I haven't done this myself before
and would question if your transfer from Madrid-Barajas to Atocha train station would be covered as part of your ticket, which I doubt. While there is a rail line from the airport to Atocha, a quick dummy booking on the Iberia website does not list Barajas to Atocha as part of the journey, just as a "change of airport". EDIT to note:
"Incluido billete para el traslado entre la estación de tren y el aeropuerto." IB does indeed cover the airport to train station transfer.
In any case you'd be looking at self-transferring any checked luggage. Additionally if you're looking to credit any miles, the train ticket will not count for Avios, BA TP, or IB EP.