I do not agree with making cities and states sanctuary cities and states like when they make them sanctuary states for illegal aliens. Either change the law or make preemption the law of the state but you shouldn't be able to flout Federal law.
Whether we agree or disagree about being "able to flout Federal law", there is room to discuss the viability, usefulness, or necessity of such acts.
Going against the law is, by definition, "illegal". But when the law itself is unjust, immoral, abusive, outdated, or tyrannical, at some point action MUST be taken. And if the government does not respond to the pressures of it's constituents to affect change through representative means, then defying such laws is the next step.
In fact, in order to challenge these laws in the courts you must first have a case where someone is charged by the State with having violated such laws with which to take to court and defend against state prosecution.
Remember Rosa Parks? She committed an illegal act by failing to vacate a row of seats in the "colored" section of a bus in favor of a white passenger in violation of Alabama segregation laws. Others had violated the same segregation laws, but the NAACP thought that she was the best candidate to affect a court challenge to those laws. At that time in our history, 90 years had elapsed since the end of the Civil War and obviously we had SERIOUS problems with Jim Crow and other various segregation laws which were specifically enacted to deprive black people of their rights. Nearly a century, with no change on the horizon through anything like representative means.
In effect, someone HAD to violate a law in order to get the law changed.
Yes...we CAN work to change such things through our representatives. However, that doesn't necessarily establish a long term, and protected, change to the law(s). We can, for example, campaign long and hard and get a particular law/statute changed through such a process. However, others can come along afterwards and work to change what we've accomplished in much the same way. We see this all the time when control shifts between Democrat and Republican in the House/Senate, both on the State and the Federal levels.
However, if you go to court and win a precedent setting court challenge? Now the laws may not only be changed as a result, other laws written to attempt the same thing may be more easily rendered null and void as a result. You have, in effect, gotten the government itself to say "nope, can't do that" through one of the three branches of the government.