Germany's most important industry has a tough new critic: Chancellor Angela Merkel.

Campaigning to win a fourth term in elections on Sunday, Merkel has turned her fire on German automakers who once could do no wrong.

Speaking at last week's Frankfurt auto show, she took executives to task for an emissions scandal that rocked Volkswagen (VLKAY) in 2015 and could spell the end of the diesel car.

"A lot of trust has been destroyed," Merkel said. "That is why the industry must do everything to win back credibility and trust, in its own interest and that of employees and German industry."

Criticism from Merkel reflects a huge shift in popular sentiment that has made cozy relations with German automakers a political liability. An opinion poll last month found that 57% of Germans had lost trust in the industry, and two-thirds felt that politicians were still too close to the carmakers.

Long celebrated for its starring role in the German economy, revelations of emissions cheating at Volkswagen (and exploitation of loopholes by others) has exposed the industry to charges that dirty diesel engines are to blame for the country's air pollution problem.

Diesel sales slump, foreign brands advance

Sales of diesel cars have slumped this year and foreign brands are taking market share from their German rivals.

In some major cities, calls to ban diesel cars entirely are gathering strength. Munich, the home of BMW (BMWYY), is considering such a ban. So is Stuttgart, where Daimler's (DMLRY) Mercedes-Benz is based.

Merkel has indicated that it's only a matter of time before Germany follows France, Britain, India and other countries in setting an expiration date for the sale of cars that use only fossil fuels.

It's a remarkable reversal of fortune for an industry that employs over 800,000 Germans -- 2% of the country's workers -- and has enjoyed close ties with the political establishment. The German state of Lower Saxony owns 20% of Volkswagen (VLKAF), for example, and elected leaders have a reputation for lobbying hard for the industry in Europe, and elsewhere.

"There is no other industry as important, and it's clear that there is a close relationship between the government and the industry," said Arndt Ellinghorst, head of Global Automotive Research at Evercore. "The government has always been lobbying for the industry."

Related: Germany says the 'diesel crisis' could hurt its economy

Merkel got her hands dirty too, and not just in lobbying European officials.

Mary Nichols, chair of the California Air Resources Board, testified before the German parliament earlier this year that Merkel had complained about the state's tough emissions standards during a 2010 meeting with former Gov. Arnold Schwarzenegger.

Nichols said she had never before experienced such an intervention.

Those tough emissions standards -- and efforts to circumvent them -- are at the core of the industry's predicament.

Diesel emits far more harmful air pollutants than gasoline, yet it was popular in Europe for decades because governments offered tax incentives in the hope of reducing CO2 emissions. (Diesel engines were thought to be more fuel efficient.) German and French automakers invested heavily in the technology.

European consumers, who were able to save on fuel costs, gravitated toward more expensive luxury models. In Germany, where both the modern car and diesel engine were born, the number of registered diesel cars had soared by 50% to 15 million since 2008, according to Commerzbank.

Related: German automakers confront a diesel rebellion in their own backyard

But Volkswagen, which owns the Audi, Skoda, SEAT and Porsche brands, could not keep pace with tougher emissions standards. U.S. prosecutors allege that its engineers knew as far back as 2006 that the company's new 2.0 liter diesel engine would not be capable of complying with regulations.

Instead of going back to the drawing board, they designed a "defeat device" software function that allowed cars to detect when their emissions were being tested, and to boost performance during that time.

The scheme went on for nearly a decade before being discovered. When it was, demand for diesels collapsed in Europe, the only market where tax incentives have made them a major factor.

"It's a purely European technology," said Ellinghorst. "You can't sell diesels in the United States and you can't sell them in China."

Throwing money at electric vehicles

German automakers are now pouring investment dollars into hybrid and electric vehicles. Volkswagen, the world's largest automaker by sales, announced last week that it will spend more than €50 billion ($60 billion) on battery cells as it pushes to electrify all 300 models in its range by 2030.

"We have got the message and we will deliver. This is not some vague declaration of intent," said CEO Matthias Müller. "The transformation in our industry is unstoppable."

Related: Germany's economic strength masks rising inequality

The German government wants to see 1 million electric cars on its roads by 2020, a goal that would require major investments in infrastructure, including charging stations.

Merkel, who is leading the polls by a wide margin, would be in prime position to help that transition if she wins Sunday's vote. The economic incentives are clear: German automakers exported 4.4 million cars in 2016.

"There is a big shift underway," said Ellinghorst. "The Germans have to move their know-how away from diesel and into electrics."