Mathematically, what is going on here?
Think of the case, when one time series is a constant equal to zero, and the second time series is stationary. You still can run regression to find the hedge ratio is zero, and you don't need to hedge a stationary price with a constant.
If two price series are cointegrated, you can run the linear regression to calculate the hedge ratio.
The linear regression is in the form: $$y = \beta X + \epsilon$$
where, $y$ is the dependent variable, $X$ is the independent variables, and $\beta$ is the slope that we want to estimate and $\epsilon$ is the error term.
In Python, the OLS function from the statsmodels package is used to calculate the hedge Ratio:
statsmodels.api.OLS (dependent_variable(y), independent_variable(X))