Per IRS Publication 17 Your Federal Income Tax (For Individuals), on page 22:
Nonresident alien or dual-status alien. Generally, a married couple can’t file a joint return if either one is a nonresident alien at any time during the tax year. However, if one spouse was a nonresident alien or dual-status alien who was married to a U.S. citizen or resident alien at the end of the year, the spouses can choose to file a joint return. If you do file a joint return, you and your spouse are both treated as U.S. residents for the entire tax year. See chapter 1 of Pub. 519, U.S. Tax Guide for Aliens.
Per IRS Publication 519 U.S. Tax Guide for Aliens, on page 9:
If, at the end of your tax year, you are married and one spouse is a U.S. citizen or a resident alien and the other spouse is a nonresident alien, you can choose to treat the nonresident spouse as a U.S. resident. This includes situations in which one spouse is a nonresident alien at the beginning of the tax year, but a resident alien at the end of the year, and the other spouse is a nonresident alien at the end of the year.
If you make this choice, you and your spouse are treated for income tax purposes as residents for your entire tax year. Neither you nor your spouse can claim under any tax treaty not to be a U.S. resident. You are both taxed on worldwide income. You must file a joint income tax return for the year you make the choice, but you and your spouse can file joint or separate returns in later years.
How To Make the Choice
Attach a statement, signed by both spouses, to your joint return for the first tax year for which the choice applies. It should contain the following information.
Additional information available in this section of IRS Publication 519:
IRS Publication 54 Tax Guide for U.S. Citizens and Resident Aliens Abroad, page 7 outlines how to obtain a Social Security Number (SSN) or an Individual Taxpayer Identification Number (ITIN) for the nonresident alien spouse chosen to be treated as a U.S. resident.
To indicate that you would like to treat your nonresident alien spouse as a resident alien for purposes of e-filing your return:
Note. In the filing steps, you will be prompted to attach a PDF of the signed statement electing to treat the nonresident alien spouse as a resident alien.
Note that any link in the information above is updated each year automatically and will take you to the most recent version of the document at the time it is accessed.