Your code works because you use an array as config. In my case i use config object:
$config = \Phalcon\Config([
   'database' => [
      'adapter' => 'Mysql',
      'host' => 'localhost',
      'username' => 'user',
      'password' => 'pass',
      'dbname' => 'db_name,
      'charset' => 'utf8mb4',
      'options' => [],
  ]
]);
$di->set(
   'dbMaster',
   function() use ($config) {
      return new \Phalcon\Db\Adapter\Pdo\Mysql((array) $config->database);
   }
);
As we see, i should use the trick (array) $config->database which converts object to array, but it doesn't converts nested arrays. My miss when created this post, here is a little bit another problem.
I don't remember where i've found such method, i tried this few years ago with Phalcon 3 and now came back to Phalcon, but now it is version 4 :)
I think all config parameters should be in a separate file, we should prevent to set it in working scripts. I can use recursive converting, but i'm not sure it is the right way.