Error on open the master key
The issue here is that SQL Server can only open the database master key if it has been encrypted with the service master key. In your scenario, the primary server can open the DB automatically because the DMK is encrypted by the SMK. When you failover, the new servers SMK was not used to encrypt the DMK so can't open it automatically.

When you run your script, it is decrypting the DMK using the password, re-generating it, then re-encrypting it using the service master key of the new server. The solution to this is quite straightforward:

sp_control_dbmasterkey_password

This system stored procedure is used to create a CREDENTIAL object in SQL Server that contains the DMK password. On start-up (or failover) SQL Server will attempt to use the SMK to decrypt the DMK. If that fails, it locates all credentials with the same family GUID as the DB being decrypted and tries each credential in turn until one is successful or no credentials are left.

Execute this stored procedure (providing the values as appropriate) on your secondary node and the next failover should see the DMK decrypted automatically.