For two decades, Warren Buffett has been decrying airlines as terrible investments. Indeed, Buffett famously observed that a far-sighted capitalist should have shot down Orville Wright's plane at Kitty Hawk to save future investors the billions of dollars they would go on to lose in the airline business.

Warren Buffett has often questioned the sustainability of airlines' profits. Image source: The Motley Fool.

Yet in a stunning reversal, Berkshire Hathaway (BRK.A -0.15%) (BRK.B 0.08%) bought shares of the three largest U.S. airlines last quarter: American Airlines (AAL -1.55%), Delta Air Lines (DAL 0.29%), and United Continental (UAL 0.02%). (Buffett revealed on Monday that Berkshire Hathaway went on to purchase shares of Southwest Airlines in October.)

This is strange timing for Berkshire Hathaway to reverse its stance on airline stocks, especially American, Delta, and United. Profit margins appear to have peaked at all three companies.

Quick gains, but what's the long-term thesis?

Depending on when during Q3 Berkshire Hathaway acquired its shares of American Airlines, Delta Air Lines, and United Continental, the conglomerate could already be sitting on substantial gains. All three stocks plunged in late June following the Brexit vote but quickly recovered. Indeed, since the beginning of July, shares of American Airlines and United Continental have rallied more than 50%.

AAL Chart

American Airlines, Delta Air Lines, and United Continental stock performance. Data by YCharts.

However, Warren Buffett is never looking for a quick trade. (These investments were probably picked by one of his two investing lieutenants: Todd Combs and Ted Wechsler. But they both subscribe to the same general investing philosophy as Buffett.) Thus, the question is why these value investors now expect strong long-term gains from airline stocks.

In Berkshire Hathaway's 2007 shareholder letter, Buffett wrote, "The worst sort of business is one that grows rapidly, requires significant capital to engender the growth, and then earns little or no money. Think airlines."

Over the past few years, American Airlines, Delta Air Lines, and United Continental have turned this logic on its head. They have increased capacity at a very modest rate, maintained double-digit pre-tax margins, and kept a lid on capital spending.

As a result, they are all producing free cash flow: especially Delta and United. All three airlines have also shown a commitment to returning cash to shareholders through buybacks, a practice that Buffett supports. These changes undoubtedly played a big role in making Berkshire Hathaway take a second look at airline stocks.

AAL Free Cash Flow (TTM) Chart

American Airlines, Delta Air Lines, and United Continental free cash flow (TTM). Data by YCharts.

Why American Airlines?

As the above chart shows, American Airlines produces much less free cash flow than Delta and United. That's because it is in the midst of an aggressive aircraft replacement program. It also has by far the weakest balance sheet of the three. And recently, it has had the lowest profit margin. Thus, American is much closer than either of its peers to the negative stereotype of airlines that Buffett painted in his 2007 Berkshire Hathaway shareholder letter.

Due to American's deteriorating fundamentals, I took advantage of the stock's rally in recent months to sell all my shares last month. Yet of Berkshire Hathaway's recent airline investments, American Airlines was the largest by a wide margin.

This is rather surprising. Of all the major airlines, Delta Air Lines seems closest to the Buffett ideal of a high-quality business. It has a solid balance sheet and produces consistent strong free cash flow. American Airlines is a much more speculative investment, and very out of character for Berkshire Hathaway.

Airline profitability may be peaking

More broadly, this is an odd time to start investing in the legacy carriers, because they are about to face severe margin pressure. For example, American Airlines and United Continental both expect to report adjusted pre-tax margins of 5%-7% this quarter. In Q4 2015, both companies earned double-digit pre-tax margins.

American Airlines expects its profit margin to fall steeply in Q4. Image source: American Airlines.

This margin pressure is likely to continue in 2017 and beyond. Airfares are finally starting to stabilize in the U.S., but the legacy carriers could face pressure on international routes from the strong dollar. To make matters worse, budget carriers are starting to crack the code on international flights, bringing new competition and undermining margins on long-haul routes.

At the same time, American Airlines, Delta Air Lines, and United Continental face surging labor costs. The industry's steady profitability in recent years has emboldened airline labor unions to demand big pay raises to make up for the sacrifices they made in the decade after 9/11. Lastly, oil prices could start rising in the coming years.

This rally could fade

Airlines certainly aren't the worst-looking stocks in the market today. Even after the huge rally of the past several months, shares of all three legacy carriers still trade at reasonable valuations.

That said, they don't look like screaming buys. Not only did I dump my American Airlines stock in October, but I also began to sell some of my United Continental shares last week. If the stocks continue to rise in the next few weeks, I may pare back my airline exposure even further.

Smaller airlines like JetBlue Airways and Alaska Air seem like better investment opportunities right now. These companies trade at similar earnings multiples to their legacy carrier peers. Yet they have higher profit margins and much greater growth opportunities, giving them far more long-term upside.