A member state of the European Union can do that, yet it is still a union. There are quite a few anti-EU parties in Europe, yet none have mustered enough support to leave yet.
At the end of the day each state does a calculus of what the benefits of staying are and what are the maluses of EU laws they don't like. If the cons outweigh the pros, the state should leave; forcing them to stay in a bad marriage is tyrannical.
> You shouldn't get to simply join when the going is good for you, and stab the rest of the nation in the back only when the going is bad. Relationships should be maintained while they are mutually beneficial, if the benefits are once sided, there is no reason to keep it.