When King Hassan of Morocco signed a document of union with Libya's Col. Muammar Qaddafi last summer, Americans were as dismayed as they were surprised. The State Department, typically understated, noted the proposed merger with "obvious concern." No wonder: seeing a longstanding American friend agree to unite his country with an active and determined enemy of the United States naturally sounds the alarms.

Such alarms may, however, be unwarranted. A close look shows that the king's move potentially strengthens his position in two ways. And if this be so, then it also favors the United States.

First, Hassan is a savvy politician who has ruled Morocco with considerable success since 1956, and he presumably took this step aware of the consequences. He has repeatedly assured Washington that the union with Libya will not weaken his ties to the United States. Why not give him the benefit of the doubt? Why assume Qaddafi got the better of him?

Recent developments support this interpretation. Qaddafi is increasingly hard- pressed as internal unrest mounts against his rule in Libya. Because of this, he appears ready to accommodate his external adversaries. In August he agreed to end his support for the Polisario movement in the Western Sahara; a month later he agreed with France to withdraw all troops from Chad. On Hassan's part, signing a unity accord may have been a way to facilitate Qaddafi's admission of defeat, by allowing him to save face.

But the union with Libya favors the United States in a second, more long-term manner, by offsetting widespread Moroccan worries about their government's close involvement with the West. The sources of this concern lie in the political culture of Morocco, especially in the Islamic religion. Islam calls on Muslims to obey a multitude of regulations covering every aspect of life from personal hygiene to the distribution of booty. Because only a Muslim can be counted on to make the necessary efforts to implement these laws (especially those dealing with justice, taxation and warfare), the religion requires that a Muslim be head of state.

Thus does Islam carry within it an imperative to rule. Non-Muslims, whether Christians, Jews, Hindus, Buddhists or others, must not control the destiny of Muslims. Although rooted in the laws of Islam, this imperative has become so widely accepted that it influences the attitudes even of secular Muslims.

Secularists are not, to be sure, interested in the application of Islamic law, but they still insist on Muslim sovereignty. This goes far to explain the acute sensitivity of Muslim peoples to any hint of encroachment by nonbelievers on their independence. To the extent a Muslim ruler appears to depend on non-Muslims, he loses credibility as a leader.

Fear of foreign encroachment limits the influence of both the American and Soviet governments. If the shah of Iran and Anwar Sadat of Egypt were perceived as too close to the United States, with this perception contributing to their downfall, current opposition movements in Afghanistan and Syria are directed at eliminating Soviet-backed regimes.

Moroccans, who are 95 percent Muslim, share this sensitivity to non-Muslim power. Their concerns were aroused in recent years by Hassan's close relations with the United States. He offered naval facilities, broadcasting stations and intelligence resources, and he received $140 million in American aid in 1984. With telling effect, the opposition accused Hassan of selling Morocco's independence to Washington and of becoming its lackey. Indeed, this challenge has become a major problem for Hassan.

It is in answer to this accusation that the union with Libya shows its value. With one stroke, the king has rendered implausible the charge that he takes orders from Washington, eliminating the strongest argument of the opposition. Even if, as American officials fear, the agreement does bestow new respectability on Qaddafi, it also potentially stabilizes Moroccan relations with the United States. Should King Hassan succeed in securing his position by throwing out this sop, the United States would have reason to be pleased about the union.

Beyond the specific effects of this accord, there is an important lesson to be drawn from these developments. All too often, American politicians hope to become the political best friend of leaders around the world. Americans encourage foreigners to love the United States, emulate the American way, provide military bases, engage in trade and so on. In mny cases, however, such high levels of involvement are inappropriate. For example, in the case of China, close ties are rendered impossible by the communist system; relations simply cannot progress beyond a certain tactical alliance, even though Americans wish them to.

Similarly, relations with Muslim governments are limited by the permanent political sensitivities of the Muslim populaces. For them, close ties arouse suspicions and provoke anti-American sentiments. Americans must realize that it is counterproductive to expect Muslim leaders to align their governments too closely with the United States. This is not just a matter of political differences but harks back to the profound Muslim anxiety about non-Muslim encroachments on power.

Relations with Muslim states must be modest -- maintained at a distance and emphasizing full freedom of action on the Muslim side. Overly visible ties hurt America's Muslim friends. Should these develop, the est remedy is a dose of amity with someone like Qaddafi, which serves to negate fear of the American hug. If the United States does not keep its distance, it has to depend on canny Muslim leaders, such as King Hassan, to do so.