There is nothing illegal about working from the E.U., assuming your visa to the E.U. permits you to do what you are doing under the relevant E.U. and national immigration laws.
The tax issues are a bit tricky. Let's set aside for a moment the remote work piece, in order to get a handle on the general rules:
When you work in the United States, your income is subject to the U.S. federal income tax and FICA and unemployment taxation and also state and local income taxes of the state where you reside.
Normally, when you earn money from working in a country other than the U.S. as a U.S. citizen, the first $120,000 a year that you earn (as of 2023, it's indexed) abroad as earned income is exempt from U.S. federal income taxation under the Foreign Earned Tax Exclusion, but is subject to whatever income taxes and social insurance taxes apply in the country where you are located. Anything earned income earned abroad excess of that is subject to both U.S. federal income taxation and to the income and social insurance taxes of the country where you are located. But, the non-U.S. income taxes that you pay are a tax credit that can be applied dollar for dollar against your U.S. federal income taxes due on that income. I don't know off hand how U.S. FICA and unemployment tax obligations are treated in that situation.
Then, there is the question of where you are working when you are doing remote work. Are you in category 1 or category 2 above?
The correct answer is generally that you are in category 2 when you are doing all of your work outside the U.S.
But this categorization isn't entirely definitive. The U.S. has one set of rules for determining your tax domicile. The country where you are located has another set of rules for determining your tax domicile. Those rules don't have to be consistent with each other. And, the country where you are located and the U.S. may have a tax treaty modifying these general rules.
Also, if you are located in a non-U.S. country for more than a brief, temporary business trip, you are going to be subject to the workplace employment laws of the place you are doing the work, regardless of your tax domicile.
Tiger Guy's answer suggests that no U.S. company will be willing to directly employ you if you are remotely working as a W-2 employee from a remote location.
Sometimes that's correct.
But I've encountered multiple medium sized and large U.S. employers who are willing to do that. And, while it is too much of an administrative burden for them if you are the only one in the company in that arrangement, in most cases, a multinational company with many similarly situated employees and offices in multiple countries may not be too bothered by this, particularly if the employer outsources its HR and tax compliance work anyway.
Representing that you are in a U.S. state when you aren't living or working in that U.S. state to avoid the labor laws and tax laws of the place where you are actually doing the work is not legal, even though it is done and sometimes people get away with it.