As the Reagan administration moves toward a harder line with Cuba, Mexico has showed its determination to maintain warm ties with Havana by signing a broad energy agreement with the government of Fidel Castro.

Initially, Mexico will help Cuba search for its own oil, sell it propane gas, expand Cuba's main refinery and assist it in buying whatever related equipment is needed on the world market.

Although Mexico has repeatedly offered to sell Cuba oil, and does sell it lubricants, Havana so far has preferred to stick to the favorable terms on which it obtains crude from the Soviet Union.

This is, nonetheles, the most comprehensive energy deal between any noncommunist country and Cuba, which depends on Moscow for 95 percent of its energy and much of its expertise. The deal also includes Mexican training of Cuban personnel.

There is no evidence that the United States has voiced opposition to the accord, although in the final days of the Carter administration and during the first week under President Reagan, Washington has toughened its stand toward Cuba, charging that Havana is aiding guerrilla movements in Central America.

Mexico, with which Reagan hopes to develop a better relationship, also has lent public diplomatic support to the opponents of the U.S.-backed government in El Salvador.

The deal was concluded Jan. 21 after several high-level missions traveled back and forth.

This month, engineers of the Mexican state oil company Pemex are expected to start work on the main refinery in Havana, which will be modernized and expanded with a new propane gas plant. The plant should make up for Cuba's severe shortages of bottled domestic gas. For this year, Mexico is to sell Cuba 10,000 tons of propane -- about one-twentieth of the amount sold to the United States last year.

But the spotlight will inevitably be on the oil exploration that Mexico is to begin both on land an off Cuba's western shore.

Soviet and Romanian engineers have found only traces of oil off shore and small quantities on land. These produce 5 percent of Cuba's oil needs. To get another, outside opinion, Castro turned to the Mexicans, who has gained specific knowledge of the area while mapping out their own structures under the Gulf of Mexico. Some believe that Cuba's offshore geological formations might be tied in, at great depth, with the vast Mexican fields.

Mexico's ongoing explorations show its oil fields off the Yucatan Peninsula reach almost as far north as Merida, which is only a few hundred miles from the Cuban west coast.

The Castro government is known to attach great political symbolism to the pact with Mexico. When the powerful head of Pemex, Jorge Diaz Serrano, visited Cuba in December for the talks, a Cuban vice president, Carlos Rafael Rodriguez, referred to the deal in glowing terms and said the Mexican aid could pave the way for Cuba's "second independence".

Cuba-watchers here say it clearly strengthens Castro's hand and weakens U.S. chances to isolate Cuba if Washington were to consider new sanctions in the face of alleged Cuban support for the region's revolutionaries.

In practical terms, the deal will also give Cuba long-wanted access to Western technology for its two refineries, both of which were built and owned by American companies until their expropriation.

Although Canadian, British and Dutch oil firms have considered providing the Cabans with technical assistance, an accord with the Mexican government is more favorable and secure from Cuba's point of view.

While the text of the agreement gives few financial details, it says that Mexico will supply "equipment and materials produced in Mexico" on a basis of "cost plus expenses" and will "make its best efforts" to help Havana acquire equipment in other countries when necessary.

U.S. law bans sale of American goods to Cuba and U.S. officials are said to be watching for evidence that Mexico might act as an intermediary in channeling American oil machinery of raw materials to Havana.

Mexico was the only Latin American nation not to break diplomatic relations with Cuba in the 1960s, but it implicitly supported the U.S.-initiated embargo imposed in 1964 by halting most of its trade with the island. However, 10 years ago it began improving political and commercial ties with Havana to the point where they are very close now. Last year, President Jose' Lopez Portillo went to Cuba following an earlier visit from President Fidel Castro here.

There has been widespread speculation as to Mexico's motives for reaching the energy agreement with Cuba. Some analysts have suggested it is merely one additional way for Mexico to assert its independence of Washington.

Others say the real reason is that is was promoted by Pemex Chief Diaz Serrano as a way of furthering his domestic political career. Diaz Serrano, a close friend of Lopez Portillo, is among several aspirants to become the next president of Mexico. One of his handicaps, these analysts say, is his image of being too pro-American and too pro-business. These analysts suggest that his trip to Havana, where he delivered a strongly pro-cuban speech, was an attempt to redress the balance.

The deal also comes at a time when Pemex has been rapidly expanding its activities abroad. It has bought the majority share in a refinery in the north of Spain and is studying joint ventures in several other nations. Pemex workers are exploring or providing technical aid in Panama, Nicaragua and Costo Rica and are also to go to China and India.