The production of hydrogen through water electrolysis facilitates large-scale energy storage, provides ancillary services, and supports the decarbonization of industries with hard-to-reduce emissions. As renewable energy adoption expands, the demand for these applications is increasing. However, water electrolysis currently faces challenges in cost competitiveness compared to other hydrogen production methods. Additionally, its reliance on potable water sources places additional strain on freshwater reserves. This study addresses these issues by exploring the economically optimal investment and operational strategy for hydrogen production using non-potable water sources. The goal is to maximize net profit through mathematical optimization, utilizing three advanced electrolysis technologies: alkaline electrolysis, proton exchange membrane electrolysis, and solid oxide electrolysis. The study examines three non-potable water sources: seawater, urban wastewater effluent, and rainwater. The model takes into account electricity market values from day-ahead and balancing markets, as well as the market values for hydrogen, oxygen, freshwater, and mixed solid salt. Two scenarios are analysed, reflecting varying levels of uncertainty in electricity price forecasts. Each scenario includes nine case studies, representing various combinations of water sources and electrolysis technologies. The results indicate that alkaline electrolysis and proton exchange membrane electrolysis are financially viable technologies, with potential annual net profits of up to 6.4 and 6.2 million euros, respectively. In contrast, solid oxide electrolysis incurs a negative net profit across all scenarios and is not economically viable under the given conditions. The main revenue sources are hydrogen sales and combined up-balancing and down-balancing services in electricity markets. Participation in the balancing market constitutes a significant portion (35 to 61%) of the total revenue in all cases with positive net profits, making it critical for economic viability. Rainwater is identified as the most cost-effective water source for hydrogen production. However, the costs associated with water treatment and brine management are minimal, contributing only 0.7 to 3.7% to the total cost of hydrogen production. Thus, differences in net profit are primarily attributed to the type of electrolysis technology used.