I thought I could do that but s/AT&T/TMobile. 3 TMobile retail stores have refused to unlock my phone. It's still unopened in a box. I paid full retail price for it and have never had a TMobile account, which is where they get confused.
In fact, I did this two years ago with the 6 Plus. I bought a T-Mobile phone from Apple because they were unlocked (at the time the others weren't, even if purchased directly from Apple) and used it with my AT&T SIM.