Elon Musk is one of America's most beloved entrepreneurs — why is he cozying up to Donald Trump?

President Donald Trump talks with Tesla and SpaceX CEO Elon Musk, center, and White House chief strategist Steve Bannon during a meeting with business leaders in the State Dining Room of the White House in Washington, Friday, Feb. 3, 2017.

President Donald Trump talks with Tesla and SpaceX CEO Elon Musk, center, and White House chief strategist Steve Bannon during a meeting with business leaders in the State Dining Room of the White House in Washington, Friday, Feb. 3, 2017.

There's a reason Elon Musk is still working closely with President Donald Trump — and it may have more to do with Tesla's bottom line than the billionaire CEO is willing to admit.

Like other tech CEOs working with Trump's economic advisory council, Musk has come under a lot of scrutiny. After Uber CEO Travis Kalanik stepped down from the council, some expected Tesla and SpaceX CEO Elon Musk to follow suit.

Instead, the environmentally conscious billionaire released a statement and took to Twitter to defend his continued participation.

"Advisory councils simply provide advice and attending does not mean that I agree with actions by the administration," Musk wrote.

Musk's willingness to associate himself with Trump has puzzled observers and industry insiders. During the 2016 election, Musk told CNBC Trump was "not the right guy" and that his character doesn't "reflect well on the United States." Musk also strongly believes that climate change is a serious threat, while Trump is known for being a climate change doubter.

On Tuesday the Huffington Post reported that both of Musk's companies, SpaceX and Tesla, have joined more than 100 other companies in an amicus brief in support of the legal effort to fight Trump's travel ban.

One theory is that Musk is trying to protect the strong government support his businesses currently enjoy.

A golden (electric) opportunity

Here's a possible reason for Musk to help Trump craft his economic agenda: Tesla, the entrepreneur's electric car company, could benefit from Trump’s protectionist trade agenda.

Unlike other auto competitors, Tesla assembles all its vehicles in the United States. A recent report from Baum & Associates LLC found that an import tax like the one floated by the Trump administration would hurt most U.S. auto companies — but would actually help Tesla be more competitive.

Tesla is also moving toward making more of its vehicles' parts within the United States as well. Last month, the company began battery-cell production at its new "Gigafactory," a Nevada-based manufacturing facility that produces the lithium ion batteries that power Tesla's cars.

"The Gigafactory would localize a major portion of vehicle cost, thus reducing their exposure to a border tax," said Alan Baum, whose firm authored the report, in an email.

According to Baum & Associates, an import tax could force automakers such as Jaguar, Land Rover and Volvo to significantly increase the prices of their vehicles, while the cost of a Tesla vehicle would remain relatively unchanged.

Federal handouts

There are other reasons for Musk to stay in the good graces of the federal government. Tesla has received hundreds of millions of dollars in guaranteed loans from Washington over the years, as part of a program that encourages innovations in fuel efficiency. A spokesperson for Musk's companies noted that Tesla had paid back its government loans.

Musk's other company SpaceX, a space exploration company, also relies on federal funds, through its partnership with NASA. That partnership was designed in part to decrease the U.S.'s reliance on Russia's space program for transportation to and from the International Space Station — a policy goal that might be less important to Trump than it was to the previous administration.

In an email, Tesla pointed to Musk's remarks at a shareholder meeting last year downplaying the importance of subsidies to the car company's business.

“Ironically, if all incentives and subsidies were removed for Tesla, Tesla’s competitive position would increase, not decrease,” Musk told shareholders, according to theNew York Times.

“Let’s say the new president erased all incentives,” Musk said, “Tesla’s competitive position would be better.”

But the New YorkTimes cast doubt on whether the immediate disappearance of incentives and subsidies would leave Tesla in a such a sustainable position: The company relies on credits for zero-emissions cars, which it can sell thanks to cap-and-trade programs in states like California.

"All that may be hard to square in the short term: Tesla turned a $22 million profit last quarter," the Times noted, "in part after selling $139 million from zero-emission vehicle credits to other car manufacturers."

Unlike Tesla, SpaceX receives funds from the federal government, not state governments.

Musk's personal defense

The theory about Musk’s decision that is regarded with the most skepticism is probably his own defense of his actions.

Musk argues that he's remaining in the White House's good graces so that he can attempt to move Trump in a "positive direction." Since Musk joined the council, Trump has already taken policy actions that Musk publicly disagrees with — including the travel ban on travelers from seven majority-Muslim countries.

"At my request," Musk tweeted after his visit with Trump, "the agenda for yesterday's White House meeting went from not mentioning the travel ban to having it be first and foremost."

On Tuesday, a federal court heard arguments on whether or notthe de facto Muslim ban should be allowed to resume while it works its way through the courts. A decision is expected by Friday, though the overall fate of the policy remains uncertain.

The same cannot be said for the fate of Tesla — whose stock is expected to rise as a result of Musk’s relationship with Trump.

Feb. 9, 2017, 4:53 p.m.: This story has been updated.

Correction: Feb. 9, 2017

An earlier version of this article misstated the source of federal funding to SpaceX. It comes via federal contracts.