Lets break it down:
I transferred XX amount of money in 2020 from my bank to my broker.
Not a taxable event unless the money was going into a IRA, Roth IRA, or a similar retirement account. Note the transfer to Roth doesn't save you taxes in 2020, but it does attach the funds to an account with rules specified in the tax code.
For the rest of the discussion I am going to assume that the money didn't go into a retirement account.
I did well in the market, and doubled it to 2 * XX in early 2021.
At some point you bought something with the funds in your account. The purchase isn't a taxable event, but the purchase does set the cost basis for your investment. As long as you didn't sell any of your investment during that time, or receive any interest or dividends, the growth isn't taxable. But if you sold anything during that time each sale or partial sale may have resulted in a capital gain or a capital loss.
At this point, I transferred half of that money, XX (the original
investment) back to my bank, keeping XX dollars in the brokerage
When you sold some or all of the investment you had a taxable event. Moving the funds from the brokerage account didn't have a tax impact, because that is turning cash into cash.
Will this trigger a capital gains tax? (I live in the U.S.A.) Would
transferring XX dollars back into my brokerage account affect my 2021
The transfer in our out of the brokerage account isn't a taxable event unless we are talking about retirement account.
Would keeping the money in the brokerage account have resulted in less
capital gains taxes?
No it wouldn't.
and from the comments.
If I lost the rest of the money in my brokerage tomorrow on stock
options, it seems like my net profit would be zero, resulting in no
taxable income. I hope I have my mind around this correctly.
If you sell an investment and aren't ready to invest in the next thing, your broker should have a safe fund that pays a little guaranteed interest. Yes that interest will be reported on your tax forms, but then again so does interest earned at a bank.
The only way to lose your money in stock options is to be invested in stock options. If that take your net income to zero depends on all the transactions attributable to that tax year. the forms you submit in April 2021 reflect all your tax gains and losses through the end of December 2020. So every sale you made in 2020 has to be accounted for. If your net capital losses in 2020 are larger than $3000 you have to carry over the excess losses to the next year.
One note about retirement accounts. Moving money into or out of the retirement accounts has to follow rules or it can have big tax consequences. If it was retirement funds then the capital gains were either deferred or tax free depending of the flavor. The transfer in or out of the brokerage account has to keep the funds in the correct type of account or you could face unexpected taxes or penalties.